import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfCell;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPRow;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

/**
 * 惠州市水产良种推进工程项目申报书
 * @author zhangmz
 */
public class AquaticProductDeclarationPDF {

	/**
	 * 第一页
	 * @param doc 	doc 内容
	 * @param bank_par  换行
	 * @param titleFont 标题字体设置
	 * @param font		内容字体设置
	 * @param date		日期
	 * @param sdf		日期格式
	 * @throws DocumentException
	 * @throws IOException
	 */
	@SuppressWarnings({  "static-access", "unchecked" })
	private static void page1(Document doc,Paragraph bank_par,Font titleFont,Font font,Date date,SimpleDateFormat sdf) throws DocumentException, IOException{
		doc.add(bank_par);
		
		Paragraph titleP = new Paragraph("惠州市水产良种推进工程\n项目申报书\n\n",
				titleFont);
		titleP.setAlignment(titleP.ALIGN_CENTER);
		doc.add(titleP);
		
		PdfPTable table = new PdfPTable(4);
		table.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.setWidthPercentage(90);
		int width[] = {20,20,9,12};//设置每列宽度比例   
		table.setWidths(width);  
		
		table.addCell(new Paragraph("项目名称：", font));
		PdfPCell cell = new PdfPCell(new Paragraph("测试项目名称啊啊啊啊啊啊啊", font));
		cell.setColspan(3);
		table.addCell(cell);
		
		table.addCell(new Paragraph("申报单位：", font));
		cell = new PdfPCell(new Paragraph("XXXXX渔业公司", font));
		cell.setColspan(3);
		table.addCell(cell);
		
		table.addCell(new Paragraph("项目主管单位：", font));
		cell = new PdfPCell(new Paragraph("县级主管单位", font));
		cell.setColspan(3);
		table.addCell(cell);
		
		table.addCell(new Paragraph("项目法人：", font));
		cell = new PdfPCell(new Paragraph("张三", font));
		cell.setColspan(3);
		table.addCell(cell);
		
		table.addCell(new Paragraph("建设时间：", font));
		cell = new PdfPCell(new Paragraph("XXXX年XXXX月XXXX日", font));
		cell.setColspan(3);
		table.addCell(cell);
		
		table.addCell(new Paragraph("联系电话：", font));
		cell = new PdfPCell(new Paragraph("18888888888", font));
		cell.setColspan(3);
		table.addCell(cell);
		
		table.addCell(new Paragraph("通讯地址：", font));
		cell = new PdfPCell(new Paragraph("惠州市惠城区云山西路凯宾斯基C座718", font));
		cell.setColspan(3);
		table.addCell(cell);
		
		for (PdfPRow row : (ArrayList<PdfPRow>) table.getRows()) {
			for (PdfPCell cells : row.getCells()) {
				if (cells != null) {
					cells.setPadding(8.0f);
					cells.setBorderWidth(0);
				}
			}
		}
		doc.add(table);
		//空白 四行
		doc.add(bank_par);
		BaseFont bf_booder = BaseFont.createFont("STSong-Light",
				"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		Font bf_booderFont = new Font(bf_booder, 18, Font.NORMAL);
		Paragraph FirstPage_booder = new Paragraph("惠州市海洋与渔业局 \n"
				+ sdf.format(date) + " 年制\n\n", bf_booderFont);
		FirstPage_booder.setAlignment(FirstPage_booder.ALIGN_CENTER);
		doc.add(FirstPage_booder);
	}
	
	private static void page2(Document doc,Font Sec_titleFont,Font font) throws DocumentException{
		doc.newPage();
		PdfPTable table = new PdfPTable(4);
		table.setWidthPercentage(90);
		table.getDefaultCell().setMinimumHeight(30);
		table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.setSplitLate(false);		
		int widths[] = {19,10,15,20};//设置每列宽度比例   
        table.setWidths(widths);  
        PdfPCell cell;
        PdfPCell cell2;
        
        cell = new PdfPCell(new PdfPCell(new Paragraph("\n\n一、项目基本信息\n\n", Sec_titleFont)));//一、项目基本信息
        cell.setColspan(4);
        cell.setBorderWidth(0);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        
        table.addCell(new PdfPCell(new Paragraph("1、项目名称", font)));
        cell = new PdfPCell(new Paragraph(" ", font));
        cell.setColspan(3);
        table.addCell(cell);
		
        table.addCell(new PdfPCell(new Paragraph("2、项目属性", font)));
        cell = new PdfPCell(new Paragraph(" ", font));
        cell.setColspan(3);
        table.addCell(cell);
        
        table.addCell(new PdfPCell(new Paragraph("3、总投资", font)));
        cell = new PdfPCell(new Paragraph(" ", font));
        cell.setColspan(3);
        table.addCell(cell);
        
        table.addCell(new PdfPCell(new Paragraph("4、申请市级财政补助", font)));
        cell = new PdfPCell(new Paragraph(" ", font));
        cell.setColspan(3);
        table.addCell(cell);
		
        cell = new PdfPCell(new Paragraph("5、项目单位", font));
        cell.setRowspan(4);
        table.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("名称：", font));
        cell.setBorderWidthRight(0);
        cell2 = new PdfPCell(new Paragraph(" ", font));
        cell2.setColspan(2);
        cell2.setBorderWidthLeft(0);
        table.addCell(cell);
        table.addCell(cell2);
        
        cell = new PdfPCell(new Paragraph("地址：", font));
        cell.setBorderWidthRight(0);
        cell2 = new PdfPCell(new Paragraph(" ", font));
        cell2.setBorderWidthLeft(0);
        cell2.setColspan(2);
        
        table.addCell(cell);
        table.addCell(cell2);
        
        cell = new PdfPCell(new Paragraph("法人代表：", font));
        cell.setBorderWidthRight(0);
        cell2 = new PdfPCell(new Paragraph(" ", font));
        cell2.setBorderWidthLeft(0);
        cell2.setColspan(2);
        
        table.addCell(cell);
        table.addCell(cell2);
        
        cell = new PdfPCell(new Paragraph("法人代表电话（手机）：", font));
        cell.setNoWrap(true);
        cell.setBorderWidthRight(0);
        cell2 = new PdfPCell(new Paragraph(" ", font));
        cell2.setBorderWidthLeft(0);
        cell2.setColspan(2);
        
        table.addCell(cell);
        table.addCell(cell2);
//      6、合作单位
        cell = new PdfPCell(new Paragraph("6、合作单位", font));
        cell.setRowspan(2);
        table.addCell(cell);
        
        cell2 = new PdfPCell(new Paragraph(" ", font));
        cell2.setColspan(3);
        table.addCell(cell2);
        
        cell2 = new PdfPCell(new Paragraph(" ", font));
        cell2.setColspan(3);
        table.addCell(cell2);
//      7、项目组成员
        cell = new PdfPCell(new Paragraph("7、项目组成员", font));
        cell.setColspan(4);
        table.addCell(cell);
        
        table.addCell(new PdfPCell(new Paragraph("姓名", font)));
        table.addCell(new PdfPCell(new Paragraph("职务", font)));
        table.addCell(new PdfPCell(new Paragraph("职称", font)));
        table.addCell(new PdfPCell(new Paragraph("所在单位", font)));
        
        for (int i = 0; i < 10; i++) {
        	 table.addCell(new PdfPCell(new Paragraph(" ", font)));
        	 table.addCell(new PdfPCell(new Paragraph(" ", font)));
        	 table.addCell(new PdfPCell(new Paragraph(" ", font)));
        	 table.addCell(new PdfPCell(new Paragraph(" ", font)));
		}
        
		for (PdfPRow row : (ArrayList<PdfPRow>) table.getRows()) {
			for (PdfPCell cells : row.getCells()) {
				if (cells != null) {
					cells.setMinimumHeight(30);
					cells.setVerticalAlignment(Element.ALIGN_MIDDLE);
				}
			}
		}
        
        doc.add(table);
	}
	
	/**
	 * 第三頁
	 * 可以重复调用
	 * @param doc
	 * @param font
	 * @param Sec_titleFont
	 * @throws DocumentException
	 * 
	 */
	private static void page3(Document doc,Font font,Font Sec_titleFont,String title,String content) throws DocumentException{
		doc.newPage();
		PdfPTable table = new PdfPTable(1);          
		table.setWidthPercentage(100);
		table.setSplitLate(false);
		String oneStr = content;
		PdfPCell cell =  new PdfPCell(new Paragraph(title,Sec_titleFont));
		cell.setMinimumHeight(30);
		//设置上下左右边框样式
		cell.setBorderWidth(1);
        cell.setBorderWidthTop(0);
        cell.setBorderWidthLeft(0);
        cell.setBorderWidthRight(0);
		table.addCell(cell);
		
		cell = new PdfPCell(new Paragraph(oneStr,font));
        cell.setMinimumHeight(700);
        cell.setBorderWidth(1);
        cell.setBorderWidthTop(0);
        table.addCell(cell);
		
        doc.add(table);
	}
	
	private static void page3(Document doc,Font Sec_titleFont,Font font) throws DocumentException{
		doc.newPage();
		PdfPTable table = new PdfPTable(1);          
		table.setWidthPercentage(100);
		table.setSplitLate(false);
		PdfPCell cell =  new PdfPCell(new Paragraph("二、项目可行性研究报告摘要",Sec_titleFont));
		cell.setMinimumHeight(30);
		//设置上下左右边框样式
		cell.setBorderWidth(1);
        cell.setBorderWidthTop(0);
        cell.setBorderWidthLeft(0);
        cell.setBorderWidthRight(0);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(cell);
//		一、目的意义
		cell = new PdfPCell(new Paragraph("一、目的意义\n",font));
        cell.setMinimumHeight(350);
        table.addCell(cell);
//      二、 现有工作基础
        cell = new PdfPCell(new Paragraph("二、 现有工作基础\n",font));
        cell.setMinimumHeight(350);
        table.addCell(cell);
        doc.add(table);
        
        doc.newPage();
        
        PdfPTable table2 = new PdfPTable(1);          
		table2.setWidthPercentage(100);
		table2.setSplitLate(false);
//		三、项目内容和目标
		cell =new PdfPCell(new Paragraph("\n\n",font));
		cell.setBorderWidth(0);
		table2.addCell(cell);
		
		cell = new PdfPCell(new Paragraph("三、项目内容和目标\n",font));
        cell.setMinimumHeight(350);
        table2.addCell(cell);
//      四、项目实施方案
        cell = new PdfPCell(new Paragraph("四、项目实施方案\n",font));
        cell.setMinimumHeight(350);
        table2.addCell(cell);
        
        doc.add(table2);
	}
	
	private static void page5(Document doc,Font Sec_titleFont,Font font,String title) throws DocumentException{
		PdfPTable table = new PdfPTable(1);     
		table.setSpacingBefore(50);
		table.setWidthPercentage(100);
		table.setSplitLate(false);
		table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
		PdfPCell cell =  new PdfPCell(new Paragraph(title,Sec_titleFont));
		cell.setMinimumHeight(30);
		/*cell.setColspan(5);*/
		cell.setBorderWidthBottom(0);
		table.addCell(cell);
		
		//内容
		cell = new PdfPCell(new Paragraph(" ",font));
		cell.setMinimumHeight(100);
		cell.setBorderWidthBottom(0);
		cell.setBorderWidthTop(0);
		table.addCell(cell);
		
		cell = new PdfPCell(new Paragraph("（单位盖章）\n\n  年     月     日	 ",font));
		cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		cell.setBorderWidthTop(0);
		table.addCell(cell);
		
		doc.add(table);
	}
	
	private static void page4(Document doc,Font Sec_titleFont,Font font) throws DocumentException{
		doc.newPage();
		Paragraph par = new Paragraph("");
		doc.add(par);
		
		PdfPTable table = new PdfPTable(4);          
		table.setWidthPercentage(100);
		table.setSplitLate(false);
		table.setSpacingBefore(200);
		table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
		PdfPCell cell;
		
		cell =  new PdfPCell(new Paragraph("五、项目经费预算（万元）",Sec_titleFont));
		cell.setColspan(4);
		table.addCell(cell);
		
		table.addCell(new Paragraph("项目",font));
		table.addCell(new Paragraph("金额(万元)",font));
		cell =  new PdfPCell(new Paragraph("用途说明",font));
		cell.setColspan(2);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(cell);
		
		for (int i = 0; i < 5; i++) {
			table.addCell(new Paragraph(" ",font));
			table.addCell(new Paragraph(" ",font));
			cell =  new PdfPCell(new Paragraph(" ",font));
			cell.setColspan(2);
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(cell);
		}
		
		cell = new PdfPCell(new Paragraph("资金构成(万元)",font));
		cell.setRowspan(2);
		table.addCell(cell);
		
		table.addCell(new Paragraph("申请市级补助",font));
		table.addCell(new Paragraph("单位自筹",font));
		table.addCell(new Paragraph("合计",font));
		//设置值
		table.addCell(new Paragraph("1111111",font));
		table.addCell(new Paragraph("2222222",font));
		table.addCell(new Paragraph("3333333",font));
		
		int i = 0;
		for (PdfPRow row : (ArrayList<PdfPRow>) table.getRows()) {
			if(i==0){
				i++;
				continue;
			}
			for (PdfPCell cells : row.getCells()) {
				
				if (cells != null) {
					cells.setPadding(8.0f);
					cells.setBorderWidth(1);
					cells.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells.setVerticalAlignment(Element.ALIGN_MIDDLE);
				}
			}
		}
		
		doc.add(table);
	}
	
	/**
	 * 主方法
	 * @throws DocumentException
	 * @throws IOException
	 */
	public static void createPDF() throws DocumentException, IOException{
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Document doc = new Document(PageSize.A4);
		PdfWriter.getInstance(doc, new FileOutputStream("d:\\惠州市水产良种推进工程项目申报书.pdf"));
		// 文档标题
		doc.addTitle("惠州市水产良种推进工程项目申报书");
		/*System.out.println(content.getItemValueAsString("项目名称"));*/
		// 文档主题
		doc.addSubject("惠州市水产良种推进工程项目申报书");
		// 文档作者
		doc.addAuthor("zmz");
		// 文档创建人
		doc.addCreator("zmz");
		// 关键字
		doc.addKeywords("惠州市水产良种推进工程项目申报书");
		doc.open();
		//标题字体设置
		BaseFont bfTitle = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
				BaseFont.NOT_EMBEDDED);
		Font titleFont = new Font(bfTitle, 26, Font.BOLD);
		//内容字体
		BaseFont bfComic = BaseFont.createFont("C:\\Windows\\Fonts\\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
		Font font = new Font(bfComic, 14, Font.NORMAL);
		Paragraph bank_par = new Paragraph("\n\n\n\n", font);
		//小标题
		Font Sec_titleFont = new Font(bfTitle, 18, Font.BOLD);
		//第一页
		page1(doc, bank_par, titleFont, font, date, sdf);
		//一、项目基本信息
		page2(doc,Sec_titleFont,font);
        //二、项目可行性研究报告摘要
		page3(doc,Sec_titleFont,font);
		
		//五、项目经费预算（万元）
		page4(doc,Sec_titleFont,font);
		//六、县区海洋与渔业主管部门意见：
		page5(doc,Sec_titleFont,font,"六、县区海洋与渔业主管部门意见：");
		//七、县区财政部门意见：
		page5(doc,Sec_titleFont,font,"七、县区财政部门意见：");
		//八、市海洋与渔业局意见
		page5(doc,Sec_titleFont,font,"八、市海洋与渔业局意见：");
		//九、市财政局意见：
		page5(doc,Sec_titleFont,font,"九、市财政局意见：");
		
		doc.close();
	}
	
	
	
	
	
	
	
	
	
	public static void main(String[] args) {
		try {
			createPDF();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
